package current.security.user.manager.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import office.BusiUtil;
import office.bo.system.Department;
import office.service.system.ISystemService;

import org.springframework.stereotype.Service;

import current.security.user.domain.Resc;
import current.security.user.domain.Role;
import current.security.user.domain.User;
import current.security.user.manager.dao.UserManagerDao;

@Service("userManagerService")
public class UserManagerService {
	@Resource
	private UserManagerDao userManagerDao;
	@Resource
	private ISystemService systemService;
	
	public void addObject(Object o){
		userManagerDao.addObject(o);
	}
	public void deleteObject(Object o){
		userManagerDao.deleteObject(o);
	}
	public void deleteR_Resc(String r_roleid){
		String hql_ = "delete from R_Resc where role.id = ?";
		userManagerDao.delete(hql_, r_roleid);
	}
	public void deleteResc(String id){
		String hql_ = "delete from R_Resc where resc.id = ?";
		userManagerDao.delete(hql_, id);
		String hql = "delete from Resc where id = ?";
		userManagerDao.delete(hql, id);
	}
	public void deleteRole(String id){
		String hql_ = "delete from R_Resc where role.id = ?";
		userManagerDao.delete(hql_ , id);
		String hql__ = "delete from U_Role where role.id = ?";
		userManagerDao.delete(hql__ , id);
		String hql = "delete from Role where id = ?";
		userManagerDao.delete(hql, id);
	}
	public void deleteUser(String id){
		String hql_ = "delete from U_Role where user.id = ?";
		userManagerDao.delete(hql_ , id);
		String hql = "delete from User where id = ?";
		userManagerDao.delete(hql, id);
	}
	
	public void deleteU_Role(String id){
		String hql_ = "delete from U_Role where user.id = ?";
		userManagerDao.delete(hql_ , id);
	}
	public Object getObjectById(Class<?> c, String id){
		return userManagerDao.getObjectById(c, id);
	}
	public List<User> getUserByRole(String roleName){
		return userManagerDao.getUserByRoleName(roleName);
	}
	public User getUserByName(String userName){
		return userManagerDao.getUserByName(userName);
	}
	
//	//先找到本部门是否有 此角色的人员.
//	public List<User> getUserByRoleAndMyDepartment(String roleName,String departmentId){
//		List<User> users = userManagerDao.getUserByRoleNameAndDepartment(roleName,departmentId);
//		if((users != null && users.size() > 0)){
//			return users;
//		}
//		return null;
//	}
	//先找到本本门是否有 此角色的人员，如果没有则继续寻找上一层部门是否有此角色
	public List<User> getUserByRoleAndDepartment(String leavel,String role,String roleName,String departmentId){
		List<User> users = userManagerDao.getUserByRoleNameAndDepartment(roleName,departmentId);
		if((users != null && users.size() > 0 && leavel != null && !leavel.equals("equal"))){
			return users;
		}else{
			//最底层的人员不允许夸部门 考核。区域，子公司经理，商务经理，行政人事经理，财务经理等可以跨部门考核
			if(!BusiUtil.validateRole(role)){
				return users;
			}
			Department pdepartment = systemService.getParent(departmentId);
			if(pdepartment != null){
				return getUserByRoleAndDepartment(leavel,role,roleName,pdepartment.getId().toString());
			}else{
				return userManagerDao.getUserByRoleName(roleName);
			}
		}
	}
	
//	//先找到本部门是否有 此角色的人员.
//	public List<User> getUserByRoleAndMyCompany(String roleName,String companyId){
//		List<User> users = userManagerDao.getUserByRoleNameAndCompany(roleName,companyId);
//		if((users != null && users.size() > 0)){
//			return users;
//		}
//		return null;
//	}
	//先找到本本门是否有 此角色的人员，如果没有则继续寻找上一层部门是否有此角色
	public List<User> getUserByRoleAndCompany(String roleName,String companyId){
		List<User> users = userManagerDao.getUserByRoleNameAndCompany(roleName,companyId);
		if((users != null && users.size() > 0)){
			return users;
		}else{
			Department pdepartment = systemService.getParent(companyId);
			if(pdepartment != null){
				return getUserByRoleAndCompany(roleName,pdepartment.getId().toString());
			}else{
				return userManagerDao.getUserByRoleName(roleName);
			}
		}
	}
	//根据部门ID 获取所有下级部门的人员列表
	public List<User> getUsersByAllDepartment(String departmentId){
		List<User> users = new ArrayList<User>();
		List<User> thisUsers = userManagerDao.getUserByDepartment(departmentId);
		users.addAll(thisUsers);
		List<Department> listDepartment = systemService.getDescendants(departmentId);
		for (Iterator<Department> iterator = listDepartment.iterator(); iterator.hasNext();) {
			Department department = (Department) iterator.next();
			List<User> listUsers = userManagerDao.getUserByDepartment(department.getId().toString());
			users.addAll(listUsers);
		}
		return users;
	}
	
	//根据部门ID 获取所有下级部门的人员列表
	public User getUserById(String userId){
		User u = (User)userManagerDao.getObjectById(User.class, userId);
		return u;
	}
	
	public void addUser(User user){
		userManagerDao.addUser(user);
	}
	
	public void updateUser(User user){
		userManagerDao.updateUser(user);
	}
	
	public Resc getRescById(Integer id){
		return userManagerDao.getRescById(id);
	}

	public List<User> getAllUser(){
		return userManagerDao.getAllUser();
	}
	/**
	 * 获取所有激活状态的人员
	 * @return
	 */
	public List<User> getAllActiveUser(){
		return userManagerDao.getAllActiveUser();
	}
	
	public List<Role> getAllRole(){
		return userManagerDao.getAllRole();
	}
	
	public List<Resc> getAllResc(){
		return userManagerDao.getAllResc();
	}
	
	public List<Resc> getRescByType(){
		return userManagerDao.getRescByType();
	}

	/**
	 * @return the userManagerDao
	 */
	public UserManagerDao getUserManagerDao() {
		return userManagerDao;
	}

	/**
	 * @param userManagerDao the userManagerDao to set
	 */
	public void setUserManagerDao(UserManagerDao userManagerDao) {
		this.userManagerDao = userManagerDao;
	}

	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

}
